//
//  EMSCharacterViewController.m
//  EVE Mac Suite
//
//  Created by Vadim Lozko on 2/6/09.
//  Copyright 2009 __Vadim__. All rights reserved.
//

#import "EMSCharacterViewController.h"
#import "EMSCharacterDataController.h"
#import "EVEAPICoreDataController.h"
#import "EVEAPICharacterSheet.h"
#import "EVEAPISkillTree.h"
#import "EMSTrainingQueueProgressBar.h"
#import "EMSGeneralUtils.h"
#import "EMSSkillTrainingInfo.h"
#import "EMSSkillPlannerWindowController.h"
#import "EVECDCharacter.h"
#import "EVECDLearnedSkill.h"
#import "EVECDSkill.h"

@implementation EMSCharacterViewController

@synthesize dataController;
@synthesize delegate;

-(id)initWithCharacterInfo:(NSDictionary*)charInfo
{
	if(self = [super init])
	{
		NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
		self.dataController = [[EMSCharacterDataController alloc] initWithCharacterInfo:charInfo];
		[self.dataController update];
		[defaultCenter addObserver:self 
						  selector:@selector(characterSheetUpdateDidFinish:) 
							  name:CHARACTER_SHEET_API_TYPE 
							object:nil];
		[defaultCenter addObserver:self
						  selector:@selector(characterQueueUpdateDidFinish:) 
							  name:CHARACTER_SKILL_QUEUE_API_TYPE 
							object:nil];
		[defaultCenter addObserver:self
						  selector:@selector(characterSheetWillUpate:)
							  name:WILL_UPDATE_LEARNED_SKILLS_NOTIFICATION_NAME 
							object:nil];
	}
	return self;
}

-(void)dealloc
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	if(skillPlannerwindowController)
	{
		[skillPlannerwindowController release];
	}
	[dataController invalidateTimers];
	[dataController release];
	[super dealloc];
}

-(NSString*)charName
{
	return [self.dataController charName];
}

-(NSManagedObjectContext*)managedObjectContext
{
	return EVECDContext;
}

-(EVECDCharacter*)character
{
	return [self.dataController managedObjectCharacter];
}
#pragma mark IBActions

-(void)performUpdate
{	
	[self.dataController forceUpdate];
}

-(void)launchSkillPlanner
{
	if(!skillPlannerwindowController)
	{
		skillPlannerwindowController = [[EMSSkillPlannerWindowController alloc]  initWithCharacterDataController:self.dataController];
		skillPlannerwindowController.delegate = self;
		[[skillPlannerwindowController window] makeKeyAndOrderFront:nil];
	} else {
		[[skillPlannerwindowController window] makeKeyAndOrderFront:nil];
	}
}

-(void)windowWillClose:(NSNotification*)notification
{
	skillPlannerwindowController = nil;
}

#pragma mark bunch of KVC Methods.

-(NSArray*)nameSortDescriptors
{
	return [EMSGeneralUtils nameSortDescriptors];
}


#pragma mark Notifications stuff

-(void)characterSheetWillUpate:(NSNotification*)notification
{
	NSString* notificationCharName = [[notification userInfo] objectForKey:CHAR_INFO_DICT_NAME];
	if([notificationCharName isEqualToString:[self charName]])
	{
		//[updateIndicator startAnimation:nil];
	}
}
		 
-(void)characterSheetUpdateDidFinish:(NSNotification*)notification
{
	NSString* notificationCharName = [[notification userInfo] objectForKey:CHAR_INFO_DICT_NAME];
	if([notificationCharName isEqualToString:[self charName]])
	{
		if([delegate respondsToSelector:@selector(didLoadCharacterViewController:)])
		{
			[delegate didLoadCharacterViewController:self];
		}
		//[updateIndicator stopAnimation:nil];
	}
}

-(void)characterQueueUpdateDidFinish:(NSNotification*)notification
{
	NSString* notificationCharName = [[notification userInfo] objectForKey:CHAR_INFO_DICT_NAME];
	if([notificationCharName isEqualToString:[self charName]])
	{
		//[updateIndicator stopAnimation:nil];
	}
}

@end
		 
@implementation EMSCharacterViewController (trainingQueueMethods)

-(NSArray*)positionSortDescriptors
{
	return [EMSGeneralUtils positionSortDescriptors];
}

@end
